﻿using Quick.CodeGenerator.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quick.CodeGenerator.CodeTemplate
{
    /// <summary>
    /// 生成模版
    /// </summary>
    /// <param name="setting"></param>
    /// <returns></returns>
    public class VueFormTemplate : ITemplate
    {
        /// <summary>
        /// 生成模版
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        public string GenerateTemplate(CodeGeneratorSetting setting, string tableName, List<ColumnInfo> columns)
        {
            StringBuilder strBuilder2 = new StringBuilder();
            strBuilder2.AppendLine($"const {tableName.Substring(0,1).ToLower()}{tableName.Substring(1)}Form = {{ ");
            int columnIndex = 0;
            //取表栏位字段属性
            foreach (Model.ColumnInfo ci in columns)
            {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append($"  {ci.COLUMN_NAME}:");
                if ("string".Equals(ci.CSharpColumnType, StringComparison.OrdinalIgnoreCase))
                {
                    strBuilder.Append(" ''");
                }
                else if ("int?".Equals(ci.CSharpColumnType, StringComparison.OrdinalIgnoreCase)
                        || "int".Equals(ci.CSharpColumnType, StringComparison.OrdinalIgnoreCase)
                        || "decimal".Equals(ci.CSharpColumnType, StringComparison.OrdinalIgnoreCase)
                        || "decimal?".Equals(ci.CSharpColumnType, StringComparison.OrdinalIgnoreCase)
                        || "double?".Equals(ci.CSharpColumnType, StringComparison.OrdinalIgnoreCase)
                        || "double".Equals(ci.CSharpColumnType, StringComparison.OrdinalIgnoreCase)
                    )
                {
                    strBuilder.Append(" 0");
                }
                else
                {
                    strBuilder.Append(" null");
                }
                columnIndex++;
                if (columnIndex < columns.Count)
                {
                    strBuilder.Append(", ");
                }
                if (!string.IsNullOrEmpty(ci.COMMENTS))
                {
                    if (strBuilder.Length < 35)
                    {
                        strBuilder.Append("                              ".Substring(0, 35 - strBuilder.Length));
                    }
                    strBuilder.Append($"//{Convert.ToString(ci.COMMENTS + "").Trim().Replace("  ", "").Replace("\r", " ").Replace("\n", " ")}");
                }
                strBuilder.Append("\r\n");

                strBuilder2.Append(strBuilder);
            }
            strBuilder2.AppendLine("}");
            return strBuilder2.ToString();
        }
    }
}
